<?php

namespace App\Http\Controllers;


use App\Clubclass;
use App\MakeTable\Table;
use App\Redict\Success;
use App\SoftImage\SoftImage;
use Illuminate\Http\Request;


class ClubclassController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $pagename = '课程管理';
        $modelurl = 'clubclasses';
        $tablecate = ['tablecate'=>'课程'];
        $headname = ['课程名称','封面图片','简介','参与人数','教练','操作'];
        $tablehead = ['title','cover_image','body','likes_count','mentor','id'];
        $clubclasstable = new Table($tablehead,$page = 8);
        $model = $clubclasstable->viewdata(Clubclass::class);
        $models = $clubclasstable->viewpagedata(Clubclass::class);
        return view('admin.home.tableshow',compact('tablecate','model','tablehead','models','headname','pagename','modelurl'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        $modelurl = 'clubclasses';
        $pagename = '新增课程';
        $formhead = ['title','cover_image','body','mentor'];
        $formname = ['课程名称','封面图片','简介','教练'];
        $formhead = collect($formname)->zip($formhead)->toArray();
        $mentors = \Auth::user()->clubmodelforrole()->mentors->pluck('name','id');
        return view('admin.home.clubclassform',compact('pagename','mentors','tags','modelurl','formhead'));
        //表单，位置 resources/views/admin/home/awardform.blade
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //添加验证
        $data = array_merge(
            ['club_id'=>\Auth::user()->clubroleid(),
                'mentor_id'=>$request->input('mentor'),
            ],
            $request->all(),
            SoftImage::cover($request->file('cover_image'))//处理图片的函数
        );
       //dd($data);
        $clubclass = Clubclass::create($data);
//        $soft->tags()->attach($request->input('tags_list')); duoduiduo

        return redirect('admin/clubclasses')->with('message', '成功添加'.$clubclass->name);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $clubclass = Clubclass::findOrFail($id);
        $modelurl = 'clubclasses';
        $pagename = $clubclass->name;
        $formhead = ['title','cover_image','body','mentor'];
        $formname = ['课程名称','封面图片','简介','教练'];
        $formhead = collect($formname)->zip($formhead)->toArray();
        $mentors = \Auth::user()->clubmodelforrole()->mentors->pluck('name','id');
        return view('admin.home.editclubclass',compact('pagename','mentors','clubclass','formhead','modelurl'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //添加验证
        $clubclass = Clubclass::findOrFail($id);
        //查找
        $data = array_merge(
            $request->all(),
            SoftImage::cover($request->file('cover_image'))//处理图片的函数
        );

        $clubclass->update($data);
        //更新
        return Success::back();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //删除
        Clubclass::destroy($id);
        return Success::back();
    }
}
